package gameSite;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/LoginJSP")
public class LoginServlet extends HttpServlet 
{
	private static final long serialVersionUID = 1L;
    public LoginServlet(){  super(); }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}

    
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		
		String user = request.getParameter("user");
		String pass = request.getParameter("pass");
		
		JDBCdriver connector = new JDBCdriver();
		Connection connection = connector.getConnectionToDatabase();
		 
		try 
		  {
			Statement queryConnection = connection.createStatement();
			String querySQL = "SELECT username, password FROM login WHERE username = \""+user+"\" AND password = \""+pass+"\";"; 
			//String querySQL = "SELECT * FROM login;";
			ResultSet resultSet = queryConnection.executeQuery(querySQL);
			//resultSet.next();
			
			if(resultSet.isBeforeFirst())
			{
				resultSet.first();
			
			if(resultSet.getString("username").equals("soen387"))
			{
				System.out.println("         --------------username: true ------------        "+resultSet.getString("username"));
			}
			else
			{
				System.out.println("         --------------username: false ------------        "+resultSet.getString("username"));
			}
			}
		  }
		  catch (SQLException e) 
		  {
			  e.printStackTrace();
		  }
	}

}
